/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2018年1月2日 下午4:42:56
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */  
package com.itttq.gshop.pbpm.provider;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;

import com.itttq.gshop.domain.po.SysUserPo;
import com.itttq.gshop.pbpm.cmpt.mybatis.CustomerSQL;

/**   
 * @ClassName:  SysUserPoSqlProvider
 * @author: 迷蝶
 * @date:   2018年1月2日 下午4:42:56 
 * @Description: TODO
 *     
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
public class SysUserSqlProvider {
	public static final String TABLE_NAME = "sys_user";

	/**
	 * 查找的SQL语句组装<br>
	 * 约定：分页两个参数为：start - 起始位置； size- 每页数量
	 * 
	 * @param sysUser
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findSysUsers(final SysUserPo sysUser) {
		return new CustomerSQL() {{
			SELECT("create_time,creator_id,email,id,mobile,password,register_ip,salt,update_time,updater_id,username ");
			FROM(TABLE_NAME);
			
			if (null != sysUser.getCreatorId()) {
				WHERE("creator_id=#{creatorId}");
			}
			if (StringUtils.isNotBlank(sysUser.getEmail())) {
				WHERE("email = #{email}");
			}
			if (StringUtils.isNotBlank(sysUser.getMobile())) {
				WHERE("mobile = #{mobile}");
			}
			if (StringUtils.isNotBlank(sysUser.getRegisterIp())) {
				WHERE("register_ip=#{registerIp}");
			}
			if (null != sysUser.getUpdaterId()) {
				WHERE("updater_id=#{updaterId}");
			}
			if (StringUtils.isNotBlank(sysUser.getUsername())) {
				WHERE("username = #{username}");
			}
			if(StringUtils.isNotBlank(sysUser.getSortField())) {
				ORDER_BY(sysUser.getSortField() + (StringUtils.isNotBlank(sysUser.getSortMethod()) ? "desc" : sysUser.getSortField()));
			} else {
				ORDER_BY("id desc");
			}
			if(null != sysUser.getStart() && null != sysUser.getSize()) {
				LIMIT(sysUser.getStart(), sysUser.getSize());
			}
		}}.toString();
	}
	
	/**
	 * 查找记录总数的SQL语句组装
	 * 
	 * @param sysUser
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findSysUserCount(final SysUserPo sysUser) {
		return new SQL() {{
			SELECT("count(*)");
			FROM(TABLE_NAME);
			if (null != sysUser.getCreatorId()) {
				WHERE("creator_id=#{creatorId}");
			}
			if (StringUtils.isNotBlank(sysUser.getEmail())) {
				WHERE("email=#{email}");
			}
			if (StringUtils.isNotBlank(sysUser.getMobile())) {
				WHERE("mobile=#{mobile}");
			}
			if (StringUtils.isNotBlank(sysUser.getPassword())) {
				WHERE("password=#{password}");
			}
			if (StringUtils.isNotBlank(sysUser.getRegisterIp())) {
				WHERE("register_ip=#{registerIp}");
			}
			if (StringUtils.isNotBlank(sysUser.getSalt())) {
				WHERE("salt=#{salt}");
			}
			if (null != sysUser.getUpdaterId()) {
				WHERE("updater_id=#{updaterId}");
			}
			if (StringUtils.isNotBlank(sysUser.getUsername())) {
				WHERE("username=#{username}");
			}
		}}.toString();
	}
	
	/**
	 * 根据ID查找的SQL语句组装
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findSysUserById(final Integer id) {
		return new SQL() {{
			SELECT("*");
			FROM(TABLE_NAME);
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 增加的SQL语句组装
	 * 
	 * @param sysUser
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String addSysUser(final SysUserPo sysUser) {
		return new SQL() {{
			INSERT_INTO(TABLE_NAME);
			if (null != sysUser.getCreatorId()) {
				VALUES("creator_id","#{creatorId}");
			}
			if (StringUtils.isNotBlank(sysUser.getEmail())) {
				VALUES("email","#{email}");
			}
			if (StringUtils.isNotBlank(sysUser.getMobile())) {
				VALUES("mobile","#{mobile}");
			}
			if (StringUtils.isNotBlank(sysUser.getPassword())) {
				VALUES("password","#{password}");
			}
			if (StringUtils.isNotBlank(sysUser.getRegisterIp())) {
				VALUES("register_ip","#{registerIp}");
			}
			if (StringUtils.isNotBlank(sysUser.getSalt())) {
				VALUES("salt","#{salt}");
			}
			if (null != sysUser.getUpdaterId()) {
				VALUES("updater_id","#{updaterId}");
			}
			if (StringUtils.isNotBlank(sysUser.getUsername())) {
				VALUES("username","#{username}");
			}
		}}.toString();
	}

	/**
	 * 修改的SQL语句组装
	 * 
	 * @param sysUser
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String updateSysUser(final SysUserPo sysUser) {
		return new SQL() {{
			UPDATE(TABLE_NAME);
			
			if (null != sysUser.getCreatorId()) {
				SET("creator_id=#{creatorId}");
			}
			if (StringUtils.isNotBlank(sysUser.getEmail())) {
				SET("email=#{email}");
			}
			if (StringUtils.isNotBlank(sysUser.getMobile())) {
				SET("mobile=#{mobile}");
			}
			if (StringUtils.isNotBlank(sysUser.getPassword())) {
				SET("password=#{password}");
			}
			if (StringUtils.isNotBlank(sysUser.getRegisterIp())) {
				SET("register_ip=#{registerIp}");
			}
			if (StringUtils.isNotBlank(sysUser.getSalt())) {
				SET("salt=#{salt}");
			}
			if (null != sysUser.getUpdaterId()) {
				SET("updater_id=#{updaterId}");
			}
			if (StringUtils.isNotBlank(sysUser.getUsername())) {
				SET("username=#{username}");
			}
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 根据ID删除的SQL语句组装
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String delSysUser(final Integer id) {
		return new SQL() {{
			DELETE_FROM(TABLE_NAME);
			WHERE("id = #{id}");
		}}.toString();
	}
}
